package persistentie;

import com.mysql.jdbc.PreparedStatement;
import domein.Antwoord;
import domein.Vraag;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author Robin
 */
public class Q_aMapper extends AbstractMapper<Integer, Vraag> {

    public final String SAVEANSWER = "INSERT INTO antwoord (vraagID, instructeurID, webinarID, tekst) VALUES(?,?,?,?)";
    public final String UPDATEANSWER = "UPDATE Antwoord SET tekst=? WHERE webinarID=? AND vraagID=?";
    public final String SAVEQUESTION = "INSERT INTO Vraag (gebruikerID, webinarID, tekst) VALUES (?,?,?)";
    public final String GETQUESTIONS = "SELECT * FROM vraag WHERE webinarID=? ORDER BY vraagID";
    public final String GETANSWERS = "SELECT tekst from Antwoord WHERE webinarID=? AND vraagID=?";
    final static String GEEFVRAGEN = "SELECT * FROM Vraag";

    @Override
    public HashMap<Integer, Vraag> geefMap() {
        HashMap<Integer, Vraag> webinars = new HashMap<Integer, Vraag>();

        try {
            PreparedStatement ps = (PreparedStatement) DbConnection.getInstance().getConnection().prepareStatement(GEEFVRAGEN);
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                int vraagID = rs.getInt("vraagID");
                int gebruikerID = rs.getInt("gebruikerID");
                int webinarID = rs.getInt("webinarID");
                String vraag = rs.getString("tekst");

                Vraag v = new Vraag(vraagID, gebruikerID, webinarID, vraag);
                webinars.put(Integer.valueOf(v.getVraagID()), v);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return webinars;
    }

    @Override
    public void saveQuestion(Vraag v) {
        try {
            PreparedStatement ps = (PreparedStatement) DbConnection.getInstance().getConnection().prepareStatement(SAVEQUESTION);
            ps.setInt(1, v.getGebruikerID());
            ps.setInt(2, v.getWebinarID());
            ps.setString(3, v.getTekst());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void SaveAnswer(int qID, int uID, int WebID, String Tekst) {
        try {
            PreparedStatement ps = (PreparedStatement) DbConnection.getInstance().getConnection().prepareStatement(SAVEANSWER);
            ps.setInt(1, qID);
            ps.setInt(2, uID);
            ps.setInt(3, WebID);
            ps.setString(4, Tekst);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public ArrayList<Vraag> getQuestions(int webID) {
        ArrayList<Vraag> Questions = new ArrayList<Vraag>();

        try {
            PreparedStatement ps = (PreparedStatement) DbConnection.getInstance().getConnection().prepareStatement(GETQUESTIONS);
            ps.setInt(1, webID);
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                Vraag qTemp = new Vraag(rs.getInt("vraagID"), rs.getInt("gebruikerID"), rs.getInt("webinarID"), rs.getString("tekst"));

                Questions.add(qTemp);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return Questions;
    }

    @Override
    public Antwoord GetAnswer(int webID, int qID) {
        Antwoord answer = new Antwoord();
        try {
            PreparedStatement ps = (PreparedStatement) DbConnection.getInstance().getConnection().prepareStatement(GETANSWERS);
            ps.setInt(1, webID);
            ps.setInt(2, qID);

            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                answer.setTekst(rs.getString("tekst"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return answer;
    }

    @Override
    public void updateAnswer(int qID, int WebID, String Tekst) {
        try {
            PreparedStatement ps = (PreparedStatement) DbConnection.getInstance().getConnection().prepareStatement(UPDATEANSWER);
            ps.setString(1, Tekst);
            ps.setInt(2, WebID);
            ps.setInt(3, qID);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
